package action;

import config.ApplicationConfig;
import server.LoginServer;
import server.SettingServer;
import server.UserServer;
import util.Decode;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.LinkedHashMap;

/**
 * User: tum0r
 * Package Name: ${PACKAGE_NAME}
 * Time: 2019-03-19 Tuesday 16:23:05
 */
public class SettingServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        LinkedHashMap<String, String> pageConfig = ApplicationConfig.readConfig("page.json");
        request.setCharacterEncoding(pageConfig.get("encode"));
        response.setCharacterEncoding(pageConfig.get("encode"));

        String settingType = request.getParameter("type");
        String settingName = request.getParameter("name");
        String settingContent = request.getParameter("content");

        HttpSession session = request.getSession();
        String username = session.getAttribute("user").toString();
        String cookie = session.getAttribute("cookie").toString();

        if (settingName != null && !settingName.equals("") && settingContent != null && !settingContent.equals("")) {
            if (username == null || username.equals("") || cookie == null || cookie.equals("")) {
                session.setAttribute("infoTitle", "未登录");
                session.setAttribute("infoContent", "您还未登录，请先登录");
                session.setAttribute("return", "index.jsp");
                session.setAttribute("panelColor", "red");
            } else {
                LoginServer loginServer = new LoginServer();
                loginServer.open();
                if (!loginServer.checkLogin(username, cookie)) {
                    loginServer.updateLoginTime(username);
                    UserServer userServer = new UserServer();
                    userServer.open();
                    int type = userServer.checkUserType(username);
                    switch (type) {
                        case 2:
                        case 3:
                            Decode decode = new Decode();
                            settingName = decode.urlDecode(settingName);
                            settingContent = decode.urlDecode(settingContent);
                            SettingServer settingServer = new SettingServer();
                            settingServer.open();
                            settingServer.recordSettingObject(settingName, settingContent);
                            settingServer.close();
                            break;
                        default:
                            session.setAttribute("infoTitle", "设置");
                            session.setAttribute("infoContent", "权限不足");
                            session.setAttribute("return", "mine");
                            session.setAttribute("panelColor", "red");
                            break;
                    }
                    userServer.close();
                } else {
                    session.setAttribute("infoTitle", "未登录");
                    session.setAttribute("infoContent", "您还未登录，请先登录");
                    session.setAttribute("return", "index.jsp");
                    session.setAttribute("panelColor", "red");
                    session.removeAttribute("user");
                    session.removeAttribute("cookie");
                }
                loginServer.close();
            }
        } else {
            session.setAttribute("infoTitle", "设置");
            session.setAttribute("infoContent", "参数不完整");
            session.setAttribute("return", "mine");
            session.setAttribute("panelColor", "red");
        }
        response.sendRedirect("information.jsp");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
